using PnFramework;
using UnityEngine;
using UnityEngine.UI;

public class LoopListTestPanel : MonoBehaviour
{
    private LoopList<Button> list;
    [SerializeField]
    private RectTransform father;

    private Vector3 mStartPos;
    private Vector3 mNextPos;
    private Vector2 mCellSize;

    private float mSpeed = 10;
    [SerializeField]
    private bool IsLoop;

    private void Start()
    {
        mNextPos = mStartPos = father.anchoredPosition;
        mCellSize = father.GetComponent<GridLayoutGroup>().cellSize;

        var btns = new Button[father.childCount];
        for (int i = 0; i < father.childCount; i++)
        {            
            btns[i] = father.GetChild(i).GetComponent<Button>();
        }          
        list = new LoopList<Button>(btns);
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            if (IsLoop || list.Vernier < list.Count - 1) list.LoopPos();
        }  
        else if (Input.GetKeyDown(KeyCode.E))
        {
            if (IsLoop || list.Vernier > 0) list.LoopNeg();
        }

        mNextPos.y = mStartPos.y + list.Vernier * mCellSize.y;        

        if ((father.position - mNextPos).sqrMagnitude <= 0.01f) return;
        father.anchoredPosition = Vector2.Lerp(father.anchoredPosition, mNextPos, Time.deltaTime * mSpeed);
    }
}